//
//  ApplicationUtil.m
//  Demo
//
//  Created by FengDongsheng on 12/6/8.
//  Copyright (c) 2012年 FengDongsheng. All rights reserved.
//

#import "ApplicationUtil.h"
#import <CoreLocation/CoreLocation.h>
#import "BeeHive.h"
#import "SunService.h"
#import "DSNetWorkStatus.h"
#import "AvoidCrash.h"

@BeeHiveService(AppConfigServiceProtocol,ApplicationUtil)

@interface ApplicationUtil()<AppConfigServiceProtocol>

@property (strong, nonatomic) UIWebView *mainWebView;
@property (strong, nonatomic, readwrite) NSDateFormatter *dateFormatter;

@end

@implementation ApplicationUtil

+ (instancetype)sharedApplicationUtil{
    static ApplicationUtil *sharedApplicationUtil = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedApplicationUtil = [[ApplicationUtil alloc] init];
    });
    return sharedApplicationUtil;
}

- (void)initializeAppSetting {
    //设成白色状态栏
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    if (@available(iOS 11.0, *)) {
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)registerAllThirdParty:(NSDictionary *)launchOptions {
    //监听网络状态
    [[NetWorkMonitor sharedNetWorkMonitor] statrMonitorNetworkState];
    [[ApplicationUtil sharedApplicationUtil] initializeAppSetting];
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1];
    #if DEBUG
    #else
        [self setAvoidCrash];
    #endif
}

//开启崩溃过滤
-(void)setAvoidCrash{
    [AvoidCrash makeAllEffective];
    NSArray *noneSelClassStrings = @[@"NSString"];
    [AvoidCrash setupNoneSelClassStringsArr:noneSelClassStrings];
    NSArray *noneSelClassPrefix = @[@"AvoidCrash"];
    [AvoidCrash setupNoneSelClassStringPrefixsArr:noneSelClassPrefix];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
}

- (void)dealwithCrashMessage:(NSNotification *)note {
    NSLog(@"崩溃了");
}

//注册push通知
- (void)registerNotification{
    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeSound
                                                        | UIUserNotificationTypeAlert
                                                        | UIUserNotificationTypeBadge
                                                                                         categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] currentUserNotificationSettings];
}

//是否开启了定位服务
- (BOOL)bIsOpenLocationService{
    BOOL bOpenLocation;
    CLAuthorizationStatus status=[CLLocationManager authorizationStatus];
    if (![CLLocationManager locationServicesEnabled] || status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
        bOpenLocation=NO;
    }else{
        bOpenLocation=YES;
    }
    return bOpenLocation;
}

/**
 *  跳转到系统设置页面，iOS8之后可用
 */
- (void)gotoSettings{
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];
    }
}

- (void)checkVersionIsTheLatest:(void(^)(BOOL needUpdate, NSInteger updateType, NSString *updateContent))completeBlock {

}

- (void)goItunesToUpdateApp {
//
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@?mt=8", APPStoreID]]];
}

- (UIImage *)launchImage {
    UIImage               *lauchImage      = nil;
    NSString              *viewOrientation = nil;
    CGSize                 viewSize        = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation     = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        viewOrientation = @"Landscape";
    } else {
        viewOrientation = @"Portrait";
    }
    
    NSArray *imagesDictionary = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary *dict in imagesDictionary) {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
            lauchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
        }
    }
    return lauchImage;
}

@end
